package 剑指offer;

/**
 * @description:
 * @author: ywk
 * @date: 2020-10-25
 */
public class 子数组的最大累加和问题 {
    public static void main(String[] args) {
        int[] arr = {1, -2, 3, -1, -1, 6, -1};
        System.out.println(maxsumofSubarray(arr));
    }

    public static int maxsumofSubarray(int[] arr) {
        // write code here
        int sum = arr[0];
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            int cur = arr[i];
            if (sum > 0) {
                sum += cur;
            } else {
                sum = cur;
            }
            if (sum > max) {
                max = sum;
            }
        }
        return max;
    }
}
